package com.cxria.springboottest.security.handler

import com.cxria.springboottest.dto.Status
import com.cxria.springboottest.enums.StatusCodeEnum
import com.cxria.springboottest.enums.StatusMsgEnum
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.AuthenticationException
import org.springframework.security.web.AuthenticationEntryPoint

import javax.servlet.ServletException
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class AuthEntryPoint implements AuthenticationEntryPoint {

    @Autowired
    private ObjectMapper objectMapper

    @Override
    void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        Status status = new Status(Status.ERROR, StatusCodeEnum.USER_NOT_LOGIN, StatusMsgEnum.USER_NOT_LOGIN)
        response.getWriter().append(objectMapper.writeValueAsString(status))
    }

}
